package com.frx.sort.bublesort;

import java.util.Arrays;

/**
 * Created with IDEA
 *
 * @author: xrf
 * @Date: 2019/10/22
 * @Time: 16:19
 */
public class TestTwo {

    public static void main(String[] args){
        int[] arr = {5,4,1,2,6,8,7};
        insertSort(arr);

        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr){
        for (int i=0;i<arr.length;i++){

            for (int j=0;j<arr.length-1-i;j++){
                if (arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] =tmp;
                }
            }
        }
    }


    public static void insertSort(int[] arr){
        for (int i = 1;i<arr.length;i++){
            int value = arr[i];
            int j = i -1;
            for (;j >= 0;j--){
                if (arr[j] > value){
                    arr[j+1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j+1] = value;
        }
    }

}
